package 富婆梦.第一阶段.第六周.练习;

import java.io.*;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第六周.练习
 * @ClassName: TestIo
 * @Author: hemengjin
 * @Description: 测试IO流
 * @Date:2021/8/29 15:45
 * @Version: 1.0
 */
public class TestIo {
    public static void main(String[] args) {
        write();
        //read();
    }

    public static void write(){
        Father father = new Father("大头爸爸",36);
        //对象流：ObjectOutputStream存档
        try {
            FileOutputStream file = new FileOutputStream("D:/tool/1.txt");
            file.write(1);
            ObjectOutputStream os = new ObjectOutputStream(file);
            //os.writeObject(father);
            System.out.println("使用FileOutputStream存档成功！");
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        //缓冲字符流：BufferedWriter存档
        try {
            File file = new File("D:/tool/2.txt");
            FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("我是一个鬼");
            bw.write("你猜一下我是什么鬼");
            bw.write("告诉你吧，我是一个机灵鬼");
            bw.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        //缓冲字节流：
        try {
            FileOutputStream file = new FileOutputStream("D:/tool/3.txt");
            BufferedOutputStream bo = new BufferedOutputStream(file);
            bo.write(1);
            bo.write(3);
            bo.write(3);
            bo.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void read(){
        //使用FileOutputStream读取
        try {
            FileInputStream file = new FileInputStream("D:/tool/1.txt");
            ObjectInputStream os = new ObjectInputStream(file);
            Father f = (Father) os.readObject();
            System.out.println("使用FileOutputStream读取："+f.name+"，"+f.age);
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        //使用BufferedWriter存档
        try {
            File file = new File("D:/tool/2.txt");
            FileReader f = new FileReader(file);
            BufferedReader br = new BufferedReader(f);
            String s = br.readLine();
            br.close();
            System.out.println(s);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
